/**
 * @ClassName: SysDiyProductPriceDao
 * @Description: 自定次产品按会员等级设置价格
 * @author hl831 wzj
 * @date 2019-12-13
 */
package com.fzrskj.cloud.free.sys.dao;

import com.fzrskj.cloud.free.core.utils.CloudContextUtils;
import com.fzrskj.cloud.free.sys.bean.SysDiyProductPriceBean;
import ms.core.consts.SysErr;
import ms.core.db.DbUtilsDrv;
import ms.core.db.IDbDrv;
import ms.core.object.CObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SysDiyProductPriceDao extends CObject {
    //private declarations
    private IDbDrv db = null;

    //constructor function to initialize database access object
    public SysDiyProductPriceDao() {
        db = new DbUtilsDrv();
    }

    //to free resource
    @Override
    public void dispose() {
        if (db != null) db.closeConnection();
        db = null;
        super.dispose();
    }

    //new record to database by map
    public int newSysDiyProductPrice(Map<String, Object> values) {
        //values.put("agentId", CloudContextUtils.getAgentId());
        int ret = db.insertRecord("sys_diy_product_price", values);
        String err = db.getErrCode();
        if (!err.equals(SysErr.CODE_SUCCESS))
            this.log(db.getMsg());

        this.setErrCode(err);
        return ret;
    }

    //new record to database by map
    public int newSysDiyProductPrice(SysDiyProductPriceBean obj) {
        Map<String, Object> values = new HashMap<>();
        values.put("productClass", obj.getProductClass());
        values.put("productCode", obj.getProductCode());
        values.put("chargeId", obj.getChargeId());
        values.put("levelCode", obj.getLevelCode());

        //values.put("agentId", CloudContextUtils.getAgentId());

        if (obj.getPrice1() >= 0) {
            values.put("price1", obj.getPrice1());
        }
        if (obj.getPrice2() >= 0) {
            values.put("price2", obj.getPrice2());
        }
        if (obj.getPrice3() >= 0) {
            values.put("price3", obj.getPrice3());
        }
        if (obj.getPrice4() >= 0) {
            values.put("price4", obj.getPrice4());
        }
        if (obj.getPrice5() >= 0) {
            values.put("price5", obj.getPrice5());
        }
        if (obj.getPrice6() >= 0) {
            values.put("price6", obj.getPrice6());
        }
        if (obj.getPrice7() >= 0) {
            values.put("price7", obj.getPrice7());
        }
        if (obj.getPrice8() >= 0) {
            values.put("price8", obj.getPrice8());
        }
        if (obj.getPrice9() >= 0) {
            values.put("price9", obj.getPrice9());
        }
        if (obj.getPrice10() >= 0) {
            values.put("price10", obj.getPrice10());
        }
        if (obj.getPrice11() >= 0) {
            values.put("price11", obj.getPrice11());
        }
        if (obj.getPricey1() >= 0) {
            values.put("pricey1", obj.getPricey1());
        }
        if (obj.getPricey2() >= 0) {
            values.put("pricey2", obj.getPricey2());
        }
        if (obj.getPricey3() >= 0) {
            values.put("pricey3", obj.getPricey3());
        }

        int ret = db.insertRecord("sys_diy_product_price", values);
        String err = db.getErrCode();
        if (!err.equals(SysErr.CODE_SUCCESS))
            this.log(db.getMsg());

        this.setErrCode(err);
        return ret;
    }

    //to delete table record by id
    public int delSysDiyProductPrice(int id) {
        String sql = "delete from sys_diy_product_price where id=? ";
        int ret = db.executeSql(sql, new Object[]{id});
        String err = db.getErrCode();
        if (!err.equals(SysErr.CODE_SUCCESS))
            this.log(db.getMsg());

        this.setErrCode(err);
        return ret;
    }

    //to delete table record by cdt
    public int delSysDiyProductPrice(Map<String, Object> cdt) {
        //cdt.put("agentId", CloudContextUtils.getAgentId());
        int ret = db.deleteRecord("sys_diy_product_price", cdt);
        String err = db.getErrCode();
        if (!err.equals(SysErr.CODE_SUCCESS))
            this.log(db.getMsg());

        this.setErrCode(err);
        return ret;
    }

    //to modify table record by values and condition
    public int modSysDiyProductPrice(Map<String, Object> values, Map<String, Object> cdt) {
        //cdt.put("agentId", CloudContextUtils.getAgentId());
        int ret = db.modifyRecord("sys_diy_product_price", values, cdt);
        String err = db.getErrCode();
        this.setErrCode(err);
        if (!err.equals(SysErr.CODE_SUCCESS))
            this.log(db.getMsg());

        return ret;
    }

    //to query sys_diy_product_price item for detail
    public SysDiyProductPriceBean querySysDiyProductPrice(int id) {
        String sql = "select * from sys_diy_product_price where id=? ";
        SysDiyProductPriceBean ret = db.queryForBean(sql, SysDiyProductPriceBean.class, new Object[]{id});
        String err = db.getErrCode();
        if (!err.equals(SysErr.CODE_SUCCESS)) {
            this.log(db.getMsg());
        }
        this.setErrCode(err);
        return ret;
    }

    //to query SysDiyProductPrice record count
    public int querySysDiyProductPrice(Map<String, Object> cdt) {
        //cdt.put("agentId", CloudContextUtils.getAgentId());
        int ret = db.queryRecordCount("sys_diy_product_price", cdt);
        this.setErrCode(db.getErrCode());
        return ret;
    }

    //to query SysDiyProductPriceBean for record list with condition,if page or pagesize less than 0, means without page
    public List<SysDiyProductPriceBean> querySysDiyProductPrice(Map<String, Object> cdt, String orderby, int page, int pagesize) {
        //cdt.put("agentId", CloudContextUtils.getAgentId());
        List<SysDiyProductPriceBean> rets = db.queryForBeans("sys_diy_product_price", "*", orderby, SysDiyProductPriceBean.class, cdt, page, pagesize);
        String err = db.getErrCode();
        if (!err.equals(SysErr.CODE_SUCCESS))
            this.log(db.getMsg());

        this.setErrCode(err);
        return rets;
    }

    //to delete table record by cdt
    public int delSysDiyProductPrice(String productClass, String productCode) {
        String sql = "delete from sys_diy_product_price where  productClass=? and productCode=?";
        int ret = db.executeSql(sql, new Object[]{productClass, productCode});
        String code = db.getErrCode();
        if (!code.equals(SysErr.CODE_SUCCESS)) {
            this.log(db.getMsg());
        }
        this.setErrCode(code);
        return ret;
    }

    //获取产品可购买时长
    public SysDiyProductPriceBean getyDiyProductPriceBuyTime(Map<String, Object> cdt) {

        //cdt.put("agentId", CloudContextUtils.getAgentId());

        StringBuilder sb = new StringBuilder("select * ");
        sb.append("from sys_diy_product_price where 1=1 ");

        int i = 0;
        Object[] params = new Object[cdt.size()];
        for (Map.Entry<String, Object> c : cdt.entrySet()) {
            sb.append(" and " + c.getKey() + "=? ");
            params[i] = c.getValue();
            i++;
        }

        SysDiyProductPriceBean view = db.queryForBean(sb.toString(), SysDiyProductPriceBean.class, params);
        String err = db.getErrCode();
        if (!err.equals(SysErr.CODE_SUCCESS)) {
            this.log(db.getMsg());
        }
        this.setErrCode(err);
        return view;
    }

    //获取产品价格
    public int getSysDiyProductPrice(String year, Map<String, Object> cdt) {
        if (!cdt.containsKey("agentId")) {
            cdt.put("agentId", CloudContextUtils.getAgentId());
        }

        int price = 0;
        StringBuilder sb = new StringBuilder(String.format("select price%s ", year));
        sb.append("from sys_diy_product_price where 1=1");
        int i = 0;
        Object[] params = new Object[cdt.size()];
        for (Map.Entry<String, Object> c : cdt.entrySet()) {
            sb.append(" and " + c.getKey() + "=?");
            params[i] = c.getValue();
            i++;
        }

        Object result = db.queryFieldValue(sb.toString(), double.class, params, 1);
        if (result != null) {
            price = Integer.parseInt(result.toString());
        }
        this.setErrCode(db.getErrCode());
        return price;
    }
}